package icu.qx.qc.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.qc.domain.QcQualityProject;
import icu.qx.qc.domain.QcQualityQproject;
import icu.qx.qc.service.IQcQualityQprojectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.qc.mapper.QcOutgoingQualityMapper;
import icu.qx.qc.domain.QcOutgoingQuality;
import icu.qx.qc.service.IQcOutgoingQualityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出货检验单Service业务层处理
 *
 * @author yong
 * @date 2024-06-26
 */
@Service
public class QcOutgoingQualityServiceImpl implements IQcOutgoingQualityService {
    @Autowired
    private QcOutgoingQualityMapper qcOutgoingQualityMapper;

    @Autowired
    private IQcQualityQprojectService qcQualityQprojectService;

    /**
     * 查询出货检验单
     *
     * @param outQuaCode 出货检验单主键
     * @return 出货检验单
     */
    @Override
    public QcOutgoingQuality selectQcOutgoingQualityByOutQuaCode(String outQuaCode) {
        return qcOutgoingQualityMapper.selectQcOutgoingQualityByOutQuaCode(outQuaCode);
    }

    /**
     * 查询出货检验单列表
     *
     * @param qcOutgoingQuality 出货检验单
     * @return 出货检验单
     */
    @Override
    public List<QcOutgoingQuality> selectQcOutgoingQualityList(QcOutgoingQuality qcOutgoingQuality) {
        return qcOutgoingQualityMapper.selectQcOutgoingQualityList(qcOutgoingQuality);
    }

    /**
     * 新增出货检验单
     *
     * @param qcOutgoingQuality 出货检验单
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int insertQcOutgoingQuality(QcOutgoingQuality qcOutgoingQuality, List<QcQualityProject> selectedProjectList) {
        qcOutgoingQuality.setCreateBy(SecurityUtils.getUsername());
        qcOutgoingQuality.setCreateTime(DateUtils.getNowDate());

        if (selectedProjectList != null && selectedProjectList.size() > 0) {
            selectedProjectList.forEach(e -> {
                e.getDrawbackList().forEach(e1 -> {
                    QcQualityQproject qcQualityQproject = new QcQualityQproject();
                    qcQualityQproject.setQqQualityCode(e.getQualityCode());
                    qcQualityQproject.setQqProCheckCode(qcOutgoingQuality.getOutQuaCode());
                    qcQualityQproject.setQqDrawbackCode(e1.getDrawbackCode());
                    qcQualityQprojectService.insertQcQualityQproject(qcQualityQproject);
                });
            });
        }

        return qcOutgoingQualityMapper.insertQcOutgoingQuality(qcOutgoingQuality);
    }

    /**
     * 修改出货检验单
     *
     * @param qcOutgoingQuality 出货检验单
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int updateQcOutgoingQuality(QcOutgoingQuality qcOutgoingQuality, List<QcQualityProject> selectedProjectList) {
        qcOutgoingQuality.setUpdateBy(SecurityUtils.getUsername());
        qcOutgoingQuality.setUpdateTime(DateUtils.getNowDate());

        // 删除所有
        qcQualityQprojectService.deleteQcQualityQprojectByQqProCheckCode(qcOutgoingQuality.getOutQuaCode());
        if (selectedProjectList != null && selectedProjectList.size() > 0) {
            selectedProjectList.forEach(e -> {
                e.getDrawbackList().forEach(e1 -> {
                    QcQualityQproject qcQualityQproject = new QcQualityQproject();
                    qcQualityQproject.setQqQualityCode(e.getQualityCode());
                    qcQualityQproject.setQqProCheckCode(qcOutgoingQuality.getOutQuaCode());
                    qcQualityQproject.setQqDrawbackCode(e1.getDrawbackCode());
                    qcQualityQprojectService.insertQcQualityQproject(qcQualityQproject);
                });
            });
        }

        return qcOutgoingQualityMapper.updateQcOutgoingQuality(qcOutgoingQuality);
    }

    /**
     * 批量删除出货检验单
     *
     * @param outQuaCodes 需要删除的出货检验单主键
     * @return 结果
     */
    @Override
    public int deleteQcOutgoingQualityByOutQuaCodes(String[] outQuaCodes) {
        return qcOutgoingQualityMapper.deleteQcOutgoingQualityByOutQuaCodes(outQuaCodes);
    }

    /**
     * 删除出货检验单信息
     *
     * @param outQuaCode 出货检验单主键
     * @return 结果
     */
    @Override
    public int deleteQcOutgoingQualityByOutQuaCode(String outQuaCode) {
        return qcOutgoingQualityMapper.deleteQcOutgoingQualityByOutQuaCode(outQuaCode);
    }
}
